package com.sgb.kjwl.view.ui.activity.home;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragCorporateWebsiteListBinding;
import com.swgk.core.view.entity.EnterpriseRecEntity;
import com.sgb.kjwl.view.ui.activity.manage.EnterpriseAndMapActivity;
import com.sgb.kjwl.view.ui.activity.manage.special.InviteMemberListActivity;
import com.sgb.kjwl.view.ui.adapter.home.CorporateWebsiteAdapter;
import com.sgb.kjwl.viewmodel.home.CorporateWebsiteModel;
import com.sgb.member.ui.activity.MemberInviteOrApplyActivity;
import com.swgk.core.base.AppLazyFragment;
import com.swgk.core.util.SharedPreferenceUtil;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.List;

/**
 * 企业网站展示列表界面
 * Create by LiWei 2021/11/24
 */
public class CorporateWebsiteListFragment extends AppLazyFragment {

    private FragCorporateWebsiteListBinding binding;
    private CorporateWebsiteModel viewModel;
    private String key = "";
    private int currentPage = 1;
    //成员adapter
    private CorporateWebsiteAdapter memberAdapter;
    private boolean isLoadMore = false;

    public static CorporateWebsiteListFragment newInstance(String key) {
        CorporateWebsiteListFragment fragment = new CorporateWebsiteListFragment();
        Bundle bundle = new Bundle();
        bundle.putString("key", key);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.frag_corporate_website_list, container, false);
        viewModel = new ViewModelProvider(this).get(CorporateWebsiteModel.class);
        return binding.getRoot();
    }

    private void requestCompanyList() {
        if (isLoadMore) {
            currentPage = currentPage + 1;
        } else {
            currentPage = 1;
        }
        viewModel.getCompanyListByType(key, currentPage);
    }

    @Override
    protected void initView() {
        memberAdapter = new CorporateWebsiteAdapter(getActivity(), null);
        binding.recycler.setAdapter(memberAdapter);

        binding.recycler.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                isLoadMore = false;
                requestCompanyList();
            }

            @Override
            public void onLoadMore() {
                isLoadMore = true;
                requestCompanyList();
            }
        });
        binding.recycler.setLayoutManager(new LinearLayoutManager(getActivity()));
       /* binding.recycler.addItemDecoration(new RecyclerCustomDecoration(
                0,
                0,
                0,
                getActivity().getResources().getDimension(R.dimen.dp0_5)
                , getActivity().getResources().getColor(R.color.color_EDEDED)));*/

        viewModel.companyList.observe(this, new Observer<List<EnterpriseRecEntity>>() {
            @Override
            public void onChanged(List<EnterpriseRecEntity> list) {
                initListView(list, isLoadMore);
            }
        });
    }

    @Override
    protected void onLazyLoad() {
        key = getArguments().getString("key", "");
        isLoadMore = false;
        requestCompanyList();
    }

    /**
     * 成员列表数据
     *
     * @param list       列表数据
     * @param isLoadMore 是否加载更多
     */
    private void initListView(List<EnterpriseRecEntity> list, boolean isLoadMore) {
        binding.recycler.refreshComplete();
        binding.recycler.loadMoreComplete();
        binding.recycler.setNoMore(false);
        if (isLoadMore) {
            if (null != memberAdapter)
                memberAdapter.addDatas(list);
            if (list.size() < 50) {
                binding.recycler.setNoMore(true);
            }
        } else {
            if (list.size() == 0) {
                setDefaultView();
            } else {
                binding.recycler.setVisibility(View.VISIBLE);
                binding.emptyView.setVisibility(View.GONE);
                if (null != memberAdapter)
                    memberAdapter.setDatas(list);
            }
        }
    }

    /**
     * 缺省页界面提示及按钮点击事件处理
     */
    public void setDefaultView() {
        binding.recycler.setVisibility(View.GONE);
        binding.emptyView.setVisibility(View.VISIBLE);
        switch (key) {
            case "2":
                binding.tvNoData.setText("您还没有成员企业，快去邀请吧！");
                binding.tvBtn1.setVisibility(View.VISIBLE);
                binding.tvBtn2.setVisibility(View.GONE);
                binding.tvBtn1.setText("邀请成员");
                binding.tvBtn1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        InviteMemberListActivity.launch(getActivity());
                    }
                });
                break;
            case "3":
                binding.tvNoData.setText("您还没有会员企业，快去邀请会员或者加盟会");
                binding.tvBtn1.setVisibility(View.VISIBLE);
                binding.tvBtn2.setVisibility(View.VISIBLE);
                binding.tvBtn1.setText("邀请会员");
                binding.tvBtn2.setText("加盟会员");
                binding.tvBtn1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        MemberInviteOrApplyActivity.startInviteOrApply(getContext(), 0);
                    }
                });
                binding.tvBtn2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        MemberInviteOrApplyActivity.startInviteOrApply(getActivity(), 1);
                    }
                });
                break;
            case "4":
                binding.tvNoData.setText("您还没有关注其他企业，快去看看吧！");
                binding.tvBtn1.setVisibility(View.VISIBLE);
                binding.tvBtn2.setVisibility(View.GONE);
                binding.tvBtn1.setText("企业大全");
                binding.tvBtn1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String longitude = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LONGITUDE_ID, "");
                        String latitude = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LATITUDE_ID, "");
                        EnterpriseAndMapActivity.start(getContext(), longitude, latitude, false);
                    }
                });
                break;
            case "5":
                binding.tvNoData.setText("暂无推荐企业");
                binding.tvBtn1.setVisibility(View.GONE);
                binding.tvBtn2.setVisibility(View.GONE);
                break;
        }
    }
}
